﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Facebook;

namespace WebApp.Models
{
    public class Album
    {
        /// <summary>
        /// A lightweight album reference whose fields haven't been populated
        /// yet
        /// </summary>
        public class Ref : Album
        {
            public Ref(String id)
            {
                this.id = id;
            }

            public void Populate(String authToken)
            {
                FacebookClient fbc = new FacebookClient(authToken);
                dynamic album = fbc.Get(id);
                Populate(album);
            }

            internal void Populate(dynamic album)
            {
                id = album["id"];
                String ownerId = album["from"]["id"];
                String ownerName = album["from"]["name"];
                owner = new Person.Ref(ownerId, ownerName);
                name = album["name"];
                String coverId = album["cover_photo"];
                cover = new Photo.Ref(coverId);
                // TODO: Figure out privacy
                privacy = Privacy.Everyone;
                count = album["count"];
                created = DateTime.Parse(album["created_time"]);
                updated = DateTime.Parse(album["updated_time"]);
                canUpload = album["can_upload"];
            }
        }

        #region Fields

        private String id = null;
        private Person.Ref owner = null;
        private String name = null;
        private Photo.Ref cover = null;
        private Privacy privacy = Privacy.Everyone;
        private long count = 0;
        private DateTime created = DateTime.Now;
        private DateTime updated = DateTime.Now;
        private bool canUpload = false;

        #endregion

        #region Properties

        /// <summary>
        /// The id of the album
        /// </summary>
        public String Id
        {
            get { return id; }
        }

        /// <summary>
        /// The owner of the album
        /// </summary>
        public Person.Ref Owner
        {
            get { return owner; }
        }

        /// <summary>
        /// The name of the album
        /// </summary>
        public String Name
        {
            get { return name; }
        }

        /// <summary>
        /// The cover photo of the album
        /// </summary>
        public Photo.Ref Cover
        {
            get { return cover; }
        }

        /// <summary>
        /// The privacy setting for the album
        /// </summary>
        public Privacy Privacy
        {
            get { return privacy; }
        }

        /// <summary>
        /// The number of photos in the album
        /// </summary>
        public long Count
        {
            get { return count; }
        }

        /// <summary>
        /// The date the album was created
        /// </summary>
        public DateTime Created
        {
            get { return created; }
        }

        /// <summary>
        /// The date the album was last updated
        /// </summary>
        public DateTime Updated
        {
            get { return updated; }
        }

        /// <summary>
        /// True if the user can upload to the album, false otherwise
        /// </summary>
        public bool CanUpload
        {
            get { return canUpload; }
        }

        #endregion

        public Album(String id, Person.Ref owner, String name, Photo.Ref cover,
            Privacy privacy, long count, DateTime created, DateTime updated,
            bool canUpload)
        {
            this.id = id;
            this.owner = owner;
            this.name = name;
            this.cover = cover;
            this.privacy = privacy;
            this.count = count;
            this.created = created;
            this.updated = updated;
            this.canUpload = canUpload;
        }

        private Album()
        {
        }

        public static Album Populate(String id, String authToken)
        {
            FacebookClient fbc = new FacebookClient(authToken);
            dynamic album = fbc.Get(id);
            return ParseObject(album);
        }

        /// <summary>
        /// Populates a list of albums from the given root id
        /// </summary>
        /// <param name="rootId">The id of the root. The url that is fetched
        /// will look like rootId/albums</param>
        /// <param name="authToken">The auth token to use</param>
        /// <returns>A list of albums</returns>
        public static List<Album> PopulateList(String rootId, String authToken)
        {
            FacebookClient fbc = new FacebookClient(authToken);
            dynamic albums = fbc.Get(rootId + "/albums");
            albums = albums["data"];
            List<Album> result = new List<Album>();
            foreach (dynamic album in albums)
            {
                result.Add(ParseObject(album));
            }
            return result;
        }

        private static Album ParseObject(dynamic album)
        {
            String id = album["id"];
            Album.Ref result = new Album.Ref(id);
            result.Populate(album);
            return result;
        }

        public List<Photo> GetPhotosInAlbum(String authToken)
        {
            FacebookClient fbc = new FacebookClient(authToken);
            dynamic photos = fbc.Get(Id + "/photos");
            photos = photos["data"];
            List<Photo> result = new List<Photo>();
            foreach (dynamic photo in photos)
            {
                String id = photo["id"];
                Photo.Ref photoRef = new Photo.Ref(id);
                photoRef.Populate(photo);
                result.Add(photoRef);
            }
            return result;
        }
    }
}